home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / zcrd.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  13.6 KB  |  455 lines

  1. /* Copyright (C) 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: zcrd.c,v 1.2 2000/09/19 19:00:53 lpd Exp $ */
  20. /* CIE color rendering operators */
  21. #include "math_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gsstruct.h"
  25. #include "gscspace.h"
  26. #include "gscolor2.h"
  27. #include "gscrd.h"
  28. #include "gscrdp.h"
  29. #include "estack.h"
  30. #include "ialloc.h"
  31. #include "idict.h"
  32. #include "idparam.h"
  33. #include "igstate.h"
  34. #include "icie.h"
  35. #include "iparam.h"
  36. #include "ivmspace.h"
  37. #include "store.h"        /* for make_null */
  38.  
  39. /* Forward references */
  40. private int zcrd1_proc_params(P2(os_ptr op, ref_cie_render_procs * pcprocs));
  41. private int zcrd1_params(P4(os_ptr op, gs_cie_render * pcrd,
  42.             ref_cie_render_procs * pcprocs, gs_memory_t * mem));
  43. private int cache_colorrendering1(P4(i_ctx_t *i_ctx_p, gs_cie_render * pcrd,
  44.                      const ref_cie_render_procs * pcprocs,
  45.                      gs_ref_memory_t * imem));
  46.  
  47. /* - currentcolorrendering <dict> */
  48. private int
  49. zcurrentcolorrendering(i_ctx_t *i_ctx_p)
  50. {
  51.     os_ptr op = osp;
  52.  
  53.     push(1);
  54.     *op = istate->colorrendering.dict;
  55.     return 0;
  56. }
  57.  
  58. /* <dict> .buildcolorrendering1 <crd> */
  59. private int
  60. zbuildcolorrendering1(i_ctx_t *i_ctx_p)
  61. {
  62.     os_ptr op = osp;
  63.     gs_memory_t *mem = gs_state_memory(igs);
  64.     int code;
  65.     es_ptr ep = esp;
  66.     gs_cie_render *pcrd;
  67.     ref_cie_render_procs procs;
  68.  
  69.     check_read_type(*op, t_dictionary);
  70.     check_dict_read(*op);
  71.     code = gs_cie_render1_build(&pcrd, mem, ".buildcolorrendering1");
  72.     if (code < 0)
  73.     return code;
  74.     code = zcrd1_params(op, pcrd, &procs, mem);
  75.     if (code < 0 ||
  76.     (code = cache_colorrendering1(i_ctx_p, pcrd, &procs,
  77.                   (gs_ref_memory_t *) mem)) < 0
  78.     ) {
  79.     rc_free_struct(pcrd, ".buildcolorrendering1");
  80.     esp = ep;
  81.     return code;
  82.     }
  83.     /****** FIX refct ******/
  84.     /*rc_decrement(pcrd, ".buildcolorrendering1"); *//* build sets rc = 1 */
  85.     istate->colorrendering.dict = *op;
  86.     make_istruct_new(op, a_readonly, pcrd);
  87.     return (esp == ep ? 0 : o_push_estack);
  88. }
  89.  
  90. /* <dict> .builddevicecolorrendering1 <crd> */
  91. private int
  92. zbuilddevicecolorrendering1(i_ctx_t *i_ctx_p)
  93. {
  94.     os_ptr op = osp;
  95.     gs_memory_t *mem = gs_state_memory(igs);
  96.     dict_param_list list;
  97.     gs_cie_render *pcrd = 0;
  98.     int code;
  99.  
  100.     check_type(*op, t_dictionary);
  101.     code = dict_param_list_read(&list, op, NULL, false, iimemory);
  102.     if (code < 0)
  103.     return code;
  104.     code = gs_cie_render1_build(&pcrd, mem, ".builddevicecolorrendering1");
  105.     if (code >= 0) {
  106.     code = param_get_cie_render1(pcrd, (gs_param_list *) & list,
  107.                      gs_currentdevice(igs));
  108.     if (code >= 0) {
  109.         /****** FIX refct ******/
  110.         /*rc_decrement(pcrd, ".builddevicecolorrendering1"); *//* build sets rc = 1 */
  111.     }
  112.     }
  113.     iparam_list_release(&list);
  114.     if (code < 0) {
  115.     rc_free_struct(pcrd, ".builddevicecolorrendering1");
  116.     return code;
  117.     }
  118.     istate->colorrendering.dict = *op;
  119.     make_istruct_new(op, a_readonly, pcrd);
  120.     return 0;
  121. }
  122.  
  123. /* <dict> <crd> .setcolorrendering1 - */
  124. private int
  125. zsetcolorrendering1(i_ctx_t *i_ctx_p)
  126. {
  127.     os_ptr op = osp;
  128.     es_ptr ep = esp;
  129.     ref_cie_render_procs procs;
  130.     int code;
  131.  
  132.     check_type(op[-1], t_dictionary);
  133.     check_stype(*op, st_cie_render1);
  134.     code = zcrd1_proc_params(op - 1, &procs);
  135.     if (code < 0)
  136.     return code;
  137.     code = gs_setcolorrendering(igs, r_ptr(op, gs_cie_render));
  138.     if (code < 0)
  139.     return code;
  140.     if (gs_cie_cs_common(igs) != 0 &&
  141.     (code = cie_cache_joint(i_ctx_p, &procs, gs_cie_cs_common(igs), igs)) < 0
  142.     )
  143.     return code;
  144.     istate->colorrendering.dict = op[-1];
  145.     istate->colorrendering.procs = procs;
  146.     pop(2);
  147.     return (esp == ep ? 0 : o_push_estack);
  148. }
  149.  
  150. /* <dict> <crd> .setdevicecolorrendering1 - */
  151. private int
  152. zsetdevicecolorrendering1(i_ctx_t *i_ctx_p)
  153. {
  154.     os_ptr op = osp;
  155.     int code;
  156.     ref_cie_render_procs procs;
  157.  
  158.     check_type(op[-1], t_dictionary);
  159.     check_stype(*op, st_cie_render1);
  160.     code = gs_setcolorrendering(igs, r_ptr(op, gs_cie_render));
  161.     if (code < 0)
  162.     return code;
  163.     refset_null((ref *)&procs, sizeof(procs) / sizeof(ref));
  164.     if (gs_cie_cs_common(igs) != 0 &&
  165.     (code = cie_cache_joint(i_ctx_p, &procs, gs_cie_cs_common(igs), igs)) < 0
  166.     )
  167.     return code;
  168.     istate->colorrendering.dict = op[-1];
  169.     refset_null((ref *)&istate->colorrendering.procs,
  170.         sizeof(istate->colorrendering.procs) / sizeof(ref));
  171.     pop(2);
  172.     return 0;
  173. }
  174.  
  175. /* Get ColorRenderingType 1 procedures from the PostScript dictionary. */
  176. private int
  177. zcrd1_proc_params(os_ptr op, ref_cie_render_procs * pcprocs)
  178. {
  179.     int code;
  180.     ref *pRT;
  181.  
  182.     if ((code = dict_proc3_param(op, "EncodeLMN", &pcprocs->EncodeLMN)) < 0 ||
  183.       (code = dict_proc3_param(op, "EncodeABC", &pcprocs->EncodeABC)) < 0 ||
  184.     (code = dict_proc3_param(op, "TransformPQR", &pcprocs->TransformPQR)) < 0
  185.     )
  186.     return (code < 0 ? code : gs_note_error(e_rangecheck));
  187.     if (dict_find_string(op, "RenderTable", &pRT) > 0) {
  188.     const ref *prte;
  189.     int size;
  190.     int i;
  191.  
  192.     check_read_type(*pRT, t_array);
  193.     size = r_size(pRT);
  194.     if (size < 5)
  195.         return_error(e_rangecheck);
  196.     prte = pRT->value.const_refs;
  197.     for (i = 5; i < size; i++)
  198.         check_proc_only(prte[i]);
  199.     make_const_array(&pcprocs->RenderTableT, a_readonly | r_space(pRT),
  200.              size - 5, prte + 5);
  201.     } else
  202.     make_null(&pcprocs->RenderTableT);
  203.     return 0;
  204. }
  205.  
  206. /* Get ColorRenderingType 1 parameters from the PostScript dictionary. */
  207. private int
  208. zcrd1_params(os_ptr op, gs_cie_render * pcrd,
  209.          ref_cie_render_procs * pcprocs, gs_memory_t * mem)
  210. {
  211.     int code;
  212.     int ignore;
  213.     gx_color_lookup_table *const prtl = &pcrd->RenderTable.lookup;
  214.     ref *pRT;
  215.  
  216.     if ((code = dict_int_param(op, "ColorRenderingType", 1, 1, 0, &ignore)) < 0 ||
  217.     (code = zcrd1_proc_params(op, pcprocs)) < 0 ||
  218.     (code = dict_matrix3_param(op, "MatrixLMN", &pcrd->MatrixLMN)) < 0 ||
  219.     (code = dict_range3_param(op, "RangeLMN", &pcrd->RangeLMN)) < 0 ||
  220.     (code = dict_matrix3_param(op, "MatrixABC", &pcrd->MatrixABC)) < 0 ||
  221.     (code = dict_range3_param(op, "RangeABC", &pcrd->RangeABC)) < 0 ||
  222.     (code = cie_points_param(op, &pcrd->points)) < 0 ||
  223.     (code = dict_matrix3_param(op, "MatrixPQR", &pcrd->MatrixPQR)) < 0 ||
  224.     (code = dict_range3_param(op, "RangePQR", &pcrd->RangePQR)) < 0
  225.     )
  226.     return code;
  227.     if (dict_find_string(op, "RenderTable", &pRT) > 0) {
  228.     const ref *prte = pRT->value.const_refs;
  229.  
  230.     /* Finish unpacking and checking the RenderTable parameter. */
  231.     check_type_only(prte[4], t_integer);
  232.     if (!(prte[4].value.intval == 3 || prte[4].value.intval == 4))
  233.         return_error(e_rangecheck);
  234.     prtl->n = 3;
  235.     prtl->m = prte[4].value.intval;
  236.     if (r_size(pRT) != prtl->m + 5)
  237.         return_error(e_rangecheck);
  238.     code = cie_table_param(pRT, prtl, mem);
  239.     if (code < 0)
  240.         return code;
  241.     } else {
  242.     prtl->table = 0;
  243.     }
  244.     pcrd->EncodeLMN = Encode_default;
  245.     pcrd->EncodeABC = Encode_default;
  246.     pcrd->TransformPQR = TransformPQR_default;
  247.     pcrd->RenderTable.T = RenderTableT_default;
  248.     return 0;
  249. }
  250.  
  251. /* Cache the results of the color rendering procedures. */
  252. private int cie_cache_render_finish(P1(i_ctx_t *));
  253. private int
  254. cache_colorrendering1(i_ctx_t *i_ctx_p, gs_cie_render * pcrd,
  255.               const ref_cie_render_procs * pcrprocs,
  256.               gs_ref_memory_t * imem)
  257. {
  258.     es_ptr ep = esp;
  259.     int code = gs_cie_render_init(pcrd);    /* sets Domain values */
  260.     int i;
  261.  
  262.     if (code < 0 ||
  263.     (code = cie_cache_push_finish(i_ctx_p, cie_cache_render_finish, imem, pcrd)) < 0 ||
  264.     (code = cie_prepare_cache3(i_ctx_p, &pcrd->DomainLMN, pcrprocs->EncodeLMN.value.const_refs, &pcrd->caches.EncodeLMN[0], pcrd, imem, "Encode.LMN")) < 0 ||
  265.     (code = cie_prepare_cache3(i_ctx_p, &pcrd->DomainABC, pcrprocs->EncodeABC.value.const_refs, &pcrd->caches.EncodeABC[0], pcrd, imem, "Encode.ABC")) < 0
  266.     ) {
  267.     esp = ep;
  268.     return code;
  269.     }
  270.     if (pcrd->RenderTable.lookup.table != 0) {
  271.     bool is_identity = true;
  272.  
  273.     for (i = 0; i < pcrd->RenderTable.lookup.m; i++)
  274.         if (r_size(pcrprocs->RenderTableT.value.const_refs + i) != 0) {
  275.         is_identity = false;
  276.         break;
  277.         }
  278.     pcrd->caches.RenderTableT_is_identity = is_identity;
  279.     if (!is_identity)
  280.         for (i = 0; i < pcrd->RenderTable.lookup.m; i++)
  281.         if ((code =
  282.              cie_prepare_cache(i_ctx_p, Range4_default.ranges,
  283.                 pcrprocs->RenderTableT.value.const_refs + i,
  284.                        &pcrd->caches.RenderTableT[i].floats,
  285.                        pcrd, imem, "RenderTable.T")) < 0
  286.             ) {
  287.             esp = ep;
  288.             return code;
  289.         }
  290.     }
  291.     return o_push_estack;
  292. }
  293.  
  294. /* Finish up after loading the rendering caches. */
  295. private int
  296. cie_cache_render_finish(i_ctx_t *i_ctx_p)
  297. {
  298.     os_ptr op = osp;
  299.     gs_cie_render *pcrd = r_ptr(op, gs_cie_render);
  300.     int code;
  301.  
  302.     if (pcrd->RenderTable.lookup.table != 0 &&
  303.     !pcrd->caches.RenderTableT_is_identity
  304.     ) {
  305.     /* Convert the RenderTableT cache from floats to fracs. */
  306.     int j;
  307.  
  308.     for (j = 0; j < pcrd->RenderTable.lookup.m; j++)
  309.         gs_cie_cache_to_fracs(&pcrd->caches.RenderTableT[j].floats,
  310.                   &pcrd->caches.RenderTableT[j].fracs);
  311.     }
  312.     pcrd->status = CIE_RENDER_STATUS_SAMPLED;
  313.     pcrd->EncodeLMN = EncodeLMN_from_cache;
  314.     pcrd->EncodeABC = EncodeABC_from_cache;
  315.     pcrd->RenderTable.T = RenderTableT_from_cache;
  316.     code = gs_cie_render_complete(pcrd);
  317.     if (code < 0)
  318.     return code;
  319.     pop(1);
  320.     return 0;
  321. }
  322.  
  323. /* ------ Internal procedures ------ */
  324.  
  325. /* Load the joint caches. */
  326. private int
  327.     cie_exec_tpqr(P1(i_ctx_t *)),
  328.     cie_post_exec_tpqr(P1(i_ctx_t *)),
  329.     cie_tpqr_finish(P1(i_ctx_t *));
  330. int
  331. cie_cache_joint(i_ctx_t *i_ctx_p, const ref_cie_render_procs * pcrprocs,
  332.         const gs_cie_common *pcie, gs_state * pgs)
  333. {
  334.     const gs_cie_render *pcrd = gs_currentcolorrendering(pgs);
  335.     gx_cie_joint_caches *pjc = gx_currentciecaches(pgs);
  336.     gs_ref_memory_t *imem = (gs_ref_memory_t *) gs_state_memory(pgs);
  337.     ref pqr_procs;
  338.     uint space;
  339.     int code;
  340.     int i;
  341.  
  342.     if (pcrd == 0)        /* cache is not set up yet */
  343.     return 0;
  344.     if (pjc == 0)        /* must already be allocated */
  345.     return_error(e_VMerror);
  346.     if (r_has_type(&pcrprocs->TransformPQR, t_null)) {
  347.     /*
  348.      * This CRD came from a driver, not from a PostScript dictionary.
  349.      * Resample TransformPQR in C code.
  350.      */
  351.     return gs_cie_cs_complete(pgs, true);
  352.     }
  353.     gs_cie_compute_points_sd(pjc, pcie, pcrd);
  354.     code = ialloc_ref_array(&pqr_procs, a_readonly, 3 * (1 + 4 + 4 * 6),
  355.                 "cie_cache_common");
  356.     if (code < 0)
  357.     return code;
  358.     /* When we're done, deallocate the procs and complete the caches. */
  359.     check_estack(3);
  360.     cie_cache_push_finish(i_ctx_p, cie_tpqr_finish, imem, pgs);
  361.     *++esp = pqr_procs;
  362.     space = r_space(&pqr_procs);
  363.     for (i = 0; i < 3; i++) {
  364.     ref *p = pqr_procs.value.refs + 3 + (4 + 4 * 6) * i;
  365.     const float *ppt = (float *)&pjc->points_sd;
  366.     int j;
  367.  
  368.     make_array(pqr_procs.value.refs + i, a_readonly | a_executable | space,
  369.            4, p);
  370.     make_array(p, a_readonly | space, 4 * 6, p + 4);
  371.     p[1] = pcrprocs->TransformPQR.value.refs[i];
  372.     make_oper(p + 2, 0, cie_exec_tpqr);
  373.     make_oper(p + 3, 0, cie_post_exec_tpqr);
  374.     for (j = 0, p += 4; j < 4 * 6; j++, p++, ppt++)
  375.         make_real(p, *ppt);
  376.     }
  377.     return cie_prepare_cache3(i_ctx_p, &pcrd->RangePQR,
  378.                   pqr_procs.value.const_refs,
  379.                   &pjc->TransformPQR[0],
  380.                   pjc, imem, "Transform.PQR");
  381. }
  382.  
  383. /* Private operator to shuffle arguments for the TransformPQR procedure: */
  384. /* v [ws wd bs bd] proc -> -mark- ws wd bs bd v proc + exec */
  385. private int
  386. cie_exec_tpqr(i_ctx_t *i_ctx_p)
  387. {
  388.     os_ptr op = osp;
  389.     const ref *ppt = op[-1].value.const_refs;
  390.     uint space = r_space(op - 1);
  391.     int i;
  392.  
  393.     check_op(3);
  394.     push(4);
  395.     *op = op[-4];        /* proc */
  396.     op[-1] = op[-6];        /* v */
  397.     for (i = 0; i < 4; i++)
  398.     make_const_array(op - 5 + i, a_readonly | space,
  399.              6, ppt + i * 6);
  400.     make_mark(op - 6);
  401.     return zexec(i_ctx_p);
  402. }
  403.  
  404. /* Remove extraneous values from the stack after executing */
  405. /* the TransformPQR procedure.  -mark- ... v -> v */
  406. private int
  407. cie_post_exec_tpqr(i_ctx_t *i_ctx_p)
  408. {
  409.     os_ptr op = osp;
  410.     uint count = ref_stack_counttomark(&o_stack);
  411.     ref vref;
  412.  
  413.     if (count < 2)
  414.     return_error(e_unmatchedmark);
  415.     vref = *op;
  416.     ref_stack_pop(&o_stack, count - 1);
  417.     *osp = vref;
  418.     return 0;
  419. }
  420.  
  421. /* Free the procs array and complete the joint caches. */
  422. private int
  423. cie_tpqr_finish(i_ctx_t *i_ctx_p)
  424. {
  425.     os_ptr op = osp;
  426.     gs_state *pgs = r_ptr(op, gs_state);
  427.     gs_cie_render *pcrd =
  428.     (gs_cie_render *)gs_currentcolorrendering(pgs);  /* break const */
  429.     int code;
  430.  
  431.     ifree_ref_array(op - 1, "cie_tpqr_finish");
  432.     pcrd->TransformPQR = TransformPQR_from_cache;
  433.     code = gs_cie_cs_complete(pgs, false);
  434.     pop(2);
  435.     return code;
  436. }
  437.  
  438. /* ------ Initialization procedure ------ */
  439.  
  440. const op_def zcrd_l2_op_defs[] =
  441. {
  442.     op_def_begin_level2(),
  443.     {"0currentcolorrendering", zcurrentcolorrendering},
  444.     {"2.setcolorrendering1", zsetcolorrendering1},
  445.     {"2.setdevicecolorrendering1", zsetdevicecolorrendering1},
  446.     {"1.buildcolorrendering1", zbuildcolorrendering1},
  447.     {"1.builddevicecolorrendering1", zbuilddevicecolorrendering1},
  448.         /* Internal "operators" */
  449.     {"1%cie_render_finish", cie_cache_render_finish},
  450.     {"3%cie_exec_tpqr", cie_exec_tpqr},
  451.     {"2%cie_post_exec_tpqr", cie_post_exec_tpqr},
  452.     {"1%cie_tpqr_finish", cie_tpqr_finish},
  453.     op_def_end(0)
  454. };
  455.